"""
This module contains the Kiwi class.
"""


from qnd import *
import time

####CONSTANTS####
GRAVITY = .35
GRAVITY_WATER = .10 #the power of gravity in water
FLY_POWER = .2
SWIM_POWER = .15
MAX_FALL_SPEED = 16
MAX_FALL_SPEED_WATER = 2
JUMP_SPEED = 4
RUN_SPEED = 4

FLOOR_TILES = (0,1,2,3)
WALL_TILES  = (0,1)
WATER_TILES = (5,6,7)


SPRITE_NAMES = [    ####ADD IMAGE NAMES TO THIS LIST FOR MORE TYPES OF TILES####
'goodkiwi1.png',
'goodkiwi2.png',
]

SPRITES=[]
for name in SPRITE_NAMES:
	image = load_image(name)
	SPRITES.append(image)

####CLASS####
class Kiwi:
	"""
	This is the class that controls the main character. Call update()
	every frame, and draw() at the end of every frame too.
	"""
	sprite = load_image('kiwi.png')
	def __init__(self, (x,y)):
		self.x,self.y=x,y
		self.xspeed = RUN_SPEED
		self.yspeed = 0
		self.sprite = SPRITES[0]
		self.width,self.height = self.sprite.get_width()-6,self.sprite.get_height()
	def update(self,input,tilemap,tilesize):
		"""Called every frame"""
		
		onfloor = self.check_floor(tilemap,tilesize)
		inwater = self.check_water(tilemap,tilesize)

		if inwater:
			maxfallspeed = MAX_FALL_SPEED_WATER
			gravity = GRAVITY_WATER
			flypower = SWIM_POWER
		else:
			maxfallspeed = MAX_FALL_SPEED
			gravity = GRAVITY
			flypower = FLY_POWER
		
		if not onfloor or self.yspeed < 0:
			if input.keys[K_UP]:
				self.yspeed -= flypower
			if self.yspeed < maxfallspeed:
				self.yspeed += gravity
			else:
				self.yspeed = maxfallspeed
		else:
			if self.yspeed > 0:
				self.yspeed = 0
				
		if onfloor and self.yspeed >= 0 and K_UP in input.keyspressed:
			self.yspeed = -JUMP_SPEED
		
		self.y += self.yspeed
		if inwater:
			if self.yspeed < -MAX_FALL_SPEED_WATER:
				self.yspeed = -MAX_FALL_SPEED_WATER
		
		if input.keys[K_LEFT]:
			if not self.check_wall_left(tilemap,tilesize):
				self.x -= self.xspeed
			self.sprite = SPRITES[0]
		if input.keys[K_RIGHT]:
			if not self.check_wall_right(tilemap,tilesize):
				self.x += self.xspeed
			self.sprite = SPRITES[1]
	def check_floor(self,tilemap,tilesize):
		"""
		Returns True if the Kiwi is standing on a solid tile.
		"""
		selfx,selfy = self.x,self.y
		startx = int(selfx)/tilesize[0]
		endx = int(selfx+self.width)/tilesize[0]
		j = int(selfy + self.height)/tilesize[1]
		
		for i in range(startx,endx+1):
			if tilemap[i][j] in FLOOR_TILES:
				distance = self.y + self.height - j*tilesize[1]
				if abs(distance) <= abs(self.yspeed):
					if self.yspeed > 0:
						self.y = j*tilesize[1]-self.height
						self.yspeed = 0
					#~ self.y = j*tilesize[1]-self.height
					return True

		return False
	def check_wall_right(self,tilemap,tilesize):
		"""
		Returns True if there is a wall directly right of the kiwi.
		"""
		selfx,selfy = self.x,self.y
		x = int(selfx + self.width)/tilesize[0]
		starty = int(selfy)/tilesize[1]
		endy = int(selfy+self.height)/tilesize[1]
		
		for y in range(starty,endy):
			if tilemap[x][y] in WALL_TILES:
				return True

		return False
	def check_wall_left(self,tilemap,tilesize):
		"""
		Returns True if there is a wall directly left of the kiwi.
		"""
		selfx,selfy = self.x,self.y		
		x = int(selfx-self.xspeed)/tilesize[0]
		starty = int(selfy)/tilesize[1]
		endy = int(selfy+self.height)/tilesize[1]
		
		for y in range(starty,endy):
			if tilemap[x][y] in WALL_TILES:
				return True

		return False
	def check_water(self,tilemap,tilesize):
		"""Returns True if the kiwi is in water"""
		selfx,selfy = self.x,self.y
		startx = int(selfx)/tilesize[0]
		endx = int(selfx+self.width)/tilesize[0]
		y = int(selfy + self.height)/tilesize[1]

		for i in range(startx,endx+1):
			for j in range(y-1,y+1):
				if tilemap[i][j] in WATER_TILES:
					return True

		return False
		
	def draw(self,screen,camera):
		"""Draw the kiwi to the screen"""
		draw_image(screen,self.sprite,(self.x-camera.x-3,int(self.y)-camera.y))
